<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: theme_modify.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
class theme_modify extends admin{
	public $obj;		#主题对象
	public $theme;		#当前主题
	public $themeAll;	#所有主题
	public $themeFile;	#主题文件
	public $errTips;	#错误提示

	function __construct() {
		parent::__construct();
		$this->main();
	}

	function main() {
		require base::load_sys_file('theme');
		if(isset($_GET['ajax'])) {
			if(in_array($_GET['ajax'], array('tpl_read', 'tpl_write'))) {
				$func = 'ajax_'.$_GET['ajax'];
				$this->$func();
			}
			exit();
		}else{
			$this->getTheme();
			if(empty($this->errTips)) {
				$this->themeFile = $this->getThemeFile();
				$this->getThemeAll();
			}
			$this->form = ADMNAME.'?mod=theme&action=modify';
			include load_tpl(__CLASS__);
		}
	}

	#读取模板代码
	function ajax_tpl_read() {
		$theme = strC($_GET, 'theme');
		$file = strC($_GET, 'file');
		if(checkLaw($theme) && checkLaw($file, false)) {
			$themeFile = TWCMS_THEME.$theme.D.$file;
			if(file_exists($themeFile)) echo f_read($themeFile);
		}
	}

	#写入模板代码
	function ajax_tpl_write() {
		$err = 1;
		$theme = strC($_POST, 'theme');
		$file = strC($_POST, 'file');
		$code = strC($_POST, 'code');
		if(!checkLaw($theme)) {
			$msg = '非法主题名';
		}elseif(!checkLaw($file, false)) {
			$msg = '非法文件名';
		}else{
			$themeDir = TWCMS_THEME.$theme.D;
			if(file_exists($themeDir) && f_write($themeDir.$file, $code)!==false) {
				$err = 0;
			}
			$msg = '保存'.($err==0 ? '成功' : '失败');
		}
		exit('{"err":"'.$err.'","msg":"'.$msg.'"}');
	}

	#获取主题文件
	function getThemeFile() {
		$fileArr = $this->getFileAll();
		$nameArr = $this->getTplName($this->theme);
		$Arr = array();
		foreach($nameArr as $file=>$name) {
			$ret = array_search($file, $fileArr);
			if($ret!==false && $ret!==null) {
				unset($fileArr[$ret]);
				$Arr[] = array('file'=>$file, 'name'=>$name);
			}
		}
		return array_merge($Arr, $fileArr);
	}

	#获取全部文件
	function getFileAll() {
		$infoFile = TWCMS_THEME.$this->theme.D.'info.ini';
		if(file_exists($infoFile)) {
			$cssArr = $this->getFile('css/', 'css');
			$incArr = $this->getFile('inc/');
			$jsArr = $this->getFile('js/', 'js');
			$htmArr = $this->getFile();
		}
		return array_merge($cssArr,$incArr,$jsArr,$htmArr);
	}

	#获取文件
	function getFile($sonDir='', $ext='htm') {
		$dir = TWCMS_THEME.$this->theme.D.$sonDir;
		$fileArr = array();
		if(is_dir($dir)) {
			if($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					$path = $dir.D.$file;
					if($file!='.' && $file!='..' && filetype($path)=='file' && substr($path, -(strlen($ext)))==$ext) {
						$fileArr[] = $sonDir.$file;
					}
				}
				closedir($dh);
			}
		}
		return $fileArr;
	}

	#获取当前主题
	function getTheme() {
		if(isset($_GET['theme'])) {
			$theme = trim($_GET['theme']);
			if(!checkLaw($theme)) {
				$this->errTips = '不正确的主题名：'.$theme;
			}elseif(!is_dir(TWCMS_THEME.$theme)) {
				$this->errTips = '主题不存在：'.$theme;
			}else{
				$this->theme = $theme;				
			}
		}else{
			$this->theme = $this->G['theme'];
		}
	}

	#获取所有主题
	function getThemeAll() {
		$this->obj = new theme();
		$this->themeAll = $this->obj->getThemeAll();
	}

	#读取中文模板名
	function getTplName($theme) {
		$sysFile = TWCMS_DATA.'system'.D.'tplname.ini';
		$nowFile = TWCMS_THEME.$theme.D.'tplname.ini';
		$sysArr = $this->getFileArr($sysFile);
		$nowArr = $this->getFileArr($nowFile);
		return array_merge($nowArr,$sysArr);
	}

	#读取文件转换成数组	
	function getFileArr($file) {
		$Arr = array();
		if(file_exists($file)) {
			$lines = file($file);
			foreach($lines as $line) {
				$line = trim($line);
				if($line && strstr($line, ',')!==false) {
					list($file, $name) = explode(',', $line);
					$Arr[trim(strip_tags($file))] = trim(strip_tags($name));
				}
				
			}
		}
		return $Arr;
	}
}